//
//  XGL_EncipherTool.swift
//  XGamesLive
//
//  Created by ios on 2024/6/4.
//

import Foundation


class XGL_EncipherTool {
    static let share: XGL_EncipherTool = XGL_EncipherTool()
    
    private let customNum: NSInteger = 10
    
    var cpk: String{
        return UserDefaults.standard.string(forKey: "PublickKey") ?? ""
    }
    var dpk: String{
        return UserDefaults.standard.string(forKey: "PrivateKey") ?? ""
    }
    
    func getPublicKey(){
        guard XGL_NetworkStatusManager.shared.isReachable else {
            return
        }
        PanRequestManager<XGL_EncipherRequestBaseResponse<Response>,XGL_CustomRequestError>().request(CustomRequestAPI(requestAPI: .publicKey)) { result in
            switch result{
            case .success(let response):
                let startPublickey = response.publicKey.subString(start: 0, end: 50)
                let endPublicKey = response.publicKey.subString(start: 50 + self.customNum, end: response.publicKey.count - self.customNum)
                UserDefaults.standard.setValue(startPublickey + endPublicKey, forKey: "PublickKey")
                let startPrivatekey = response.privateKey.subString(start: 0, end: 50)
                let centerPrivateKey = response.privateKey.subString(start: 50 + self.customNum, end: response.privateKey.count - 50 - self.customNum)
                let endPrivateKey = response.privateKey.subString(start: response.privateKey.count - 50, end: response.privateKey.count)
//                self.dpk = startPrivatekey + centerPrivateKey + endPrivateKey
                UserDefaults.standard.setValue(startPrivatekey + centerPrivateKey + endPrivateKey, forKey: "PrivateKey")
                break
            default:
                break
            }
        }
    }
}

import PanKit
import SmartCodable

class XGL_EncipherRequestBaseResponse<T: SmartCodable>: PanBaseReponseProtocol {
    
    typealias Response = T
    
    var resultData: T? = nil
    var resultPageDatas: [T]? = nil //page
    var resultDatas: [T]? = nil
    /// request result
    var responseResult: Bool?
    var resultCode: String = "0x200"
    var resultMessage: String = "Network request failed"
    var resultOther: String = ""
    var resultJson: String = ""
    var resultDic: [String:SmartAny<Any>]?
    var resultArray: [SmartAny<Any>]?
    var resultBool: Bool?
    var resultJsonData: Data?
    
    var publicKey: String = ""
    var privateKey: String = ""
    
    func result(arg: T) -> T? {
        return self.resultData
    }
    
    func result(arg: T) -> [T]? {
        return self.resultDatas
    }
    
    func code() -> String {
        guard let result = self.responseResult else { return self.resultCode }
        return result ? "0x200" : "0x10000"
    }
    
    func message() -> String {
        return self.resultMessage
    }
    
    func other() -> String {
        return self.resultOther
    }

    //MARK: 自定义映射规则
    static func mappingForKey() -> [SmartKeyTransformer]? {
        var keys: [SmartKeyTransformer] = [
            CodingKeys.resultData       <--- PanRequestConfig.share.responseReult,
            CodingKeys.responseResult   <--- PanRequestConfig.share.requestResult,
            CodingKeys.resultPageDatas  <--- "\(PanRequestConfig.share.responseReult).records",
            CodingKeys.resultDatas      <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultCode       <--- PanRequestConfig.share.responseCode,
            CodingKeys.resultMessage    <--- PanRequestConfig.share.responseMessage,
            CodingKeys.resultJson       <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultJsonData   <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultDic        <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultArray      <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultBool       <--- PanRequestConfig.share.responseReult,
            CodingKeys.publicKey        <--- "content",
            CodingKeys.privateKey       <--- "content2"
        ]
        if let otherKey = PanRequestConfig.share.responseOther {
            keys.append(CodingKeys.resultOther  <--- otherKey)
        }
        return keys
    }
    
    required init() {
    }
}
